package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.CountByTimeDto;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.ReservationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;


@Service
@Slf4j
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements ReservationService {


    @Autowired
    private ReservationMapper reservationMapper;

    @Override
    public int getCancelledCount() {
      Long userId = UserThreadLocal.getUserId();
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        QueryWrapper<Reservation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("create_by", userId);
        queryWrapper.eq("status",2 );
        queryWrapper.eq("type", 0);
        queryWrapper.between("create_time", startTime, endTime);
        List<Reservation> list = reservationMapper.selectList(queryWrapper);
        if (CollUtil.isEmpty(list)){
            return 0;
        }else if (list.size()>3){
            throw new BaseException("预约次数已达上限");
        }else {
            return list.size();
        }
    }

    @Override
    public List<CountByTimeDto> countReservationsForTime(Long time) {
        // 将时间戳转换为 Instant 对象
        Instant instant = Instant.ofEpochMilli(time);
        // 将 Instant 对象转换为 LocalDateTime 对象，使用系统默认时区
        LocalDateTime datetime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        LocalDate localDate = datetime.toLocalDate();
         List<CountByTimeDto> countByTimeDtos = reservationMapper.countByTime(localDate);
         if (CollUtil.isEmpty(countByTimeDtos)){
             return null;
         }
           return countByTimeDtos;
    }

    @Override
    public void add(Reservation reservation) {
        reservation.setStatus(0);
        reservation.setCreateBy(String.valueOf(UserThreadLocal.getUserId()));
        log.info("add:{}",reservation);
        reservationMapper.add(reservation);
    }

    @Override
    public List<Reservation> selectReservationList(Reservation reservation) {
        List<Reservation> list = reservationMapper.pageList(reservation);
        return list;
    }

    @Override
    public void updateStatus(Long id) {
        UpdateWrapper<Reservation> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.eq("status","0");
        updateWrapper.eq("type","0");
        List<Reservation> resultList = reservationMapper.selectList(updateWrapper);
        if (CollUtil.isEmpty(resultList)){
            throw new BaseException("没有未完成的预约");
        }else{
            Reservation reservation = new Reservation();
           reservation.setStatus(2);
           reservationMapper.update(reservation,updateWrapper);
        }
    }


}
